package com.linkage.jz.bankruptcy.activity.Project;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.adapter.CommonTabPagerAdapter;
import com.linkage.jz.bankruptcy.bean.MyProInfo;
import com.linkage.jz.bankruptcy.fragment.Project.ProDetailFragment;
import com.linkage.jz.bankruptcy.fragment.Project.ProFlowFragment;
import com.linkage.jz.bankruptcy.fragment.Project.ProTaskFragment;
import com.linkage.jz.bankruptcy.fragment.Project.ProPublicFragment;
import com.linkage.jz.bankruptcy.fragment.Project.ProTalkFragment;
import com.linkage.jz.bankruptcy.fragment.Project.ProVoteFragment;
import com.linkage.jz.bankruptcy.fragment.Project.ProZQInfoFragment;
import com.linkage.jz.bankruptcy.fragment.Project.ProZQSetFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Arrays;

import jp.wasabeef.glide.transformations.CropCircleTransformation;


public class ProZQDetailActivity extends AppCompatActivity implements CommonTabPagerAdapter.TabPagerListener {

    Toolbar toolbar;
    CollapsingToolbarLayout collapsingToolbar;
    TabLayout tabLayout;
    AppBarLayout appbar;
    ViewPager viewpager;
    TextView tv_proname,tv_managename,tv_manageaddr,tv_manageuser;
    ImageView iv_proimg;

    private CommonTabPagerAdapter adapter;
    private  MyProInfo mInfo;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scrolling, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.toolbar_r_1) {
            CreateProjectDetailActivity.start(this,mInfo,2);
            return true;
        }

        if (id == R.id.toolbar_r_2) {
            PermissionActivity.start(this,mInfo);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    //
    /**
     * 设置屏幕透明度
     * @param alpha
     */
    private void setWindowAlpha(float alpha) {
        WindowManager.LayoutParams lp =getWindow().getAttributes();
//        lp.alpha = alpha; //0.0-1.0
//       getWindow().setAttributes(lp);
    }


    public int Dp2Dx(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            //透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        }
        if (getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_zq_detail);

        String bookJson=getIntent().getStringExtra("info");

        mInfo=new Gson().fromJson(bookJson,MyProInfo.class);


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        appbar = (AppBarLayout) findViewById(R.id.appbar);
        viewpager = (ViewPager) findViewById(R.id.viewpager);

        tv_proname = (TextView) findViewById(R.id.tv_proname);
        tv_managename = (TextView) findViewById(R.id.tv_managename);
        tv_manageaddr = (TextView) findViewById(R.id.tv_manageaddr);
        tv_manageuser = (TextView) findViewById(R.id.tv_manageuser);
        iv_proimg = (ImageView)findViewById(R.id.iv_proimg);

        if(mInfo!=null)
        {
            tv_proname.setText(mInfo.getProjectName());
            tv_manageaddr.setText(mInfo.getManagerListStr());
            tv_manageuser.setText(mInfo.getDebtorLegalRepresentative());

            String url = mInfo.getProjectLogo();

            Glide.with(this).load(url)
                    .bitmapTransform(new CropCircleTransformation(this))
                    .placeholder(R.drawable.avatar_defult)
                    .into(iv_proimg);
        }
//        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        //2.MODE_FIXED模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);


        setTitle("");
        collapsingToolbar.setTitle("");
        collapsingToolbar.setExpandedTitleColor(Color.parseColor("#00ffffff"));//设置还没收缩时状态下字体颜色
        collapsingToolbar.setCollapsedTitleTextColor(Color.WHITE);//设置收缩后Toolbar上字体的
        adapter = new CommonTabPagerAdapter(getSupportFragmentManager()
                , 3, Arrays.asList("基础信息", "债权信息", "操作记录"), this);
        adapter.setListener(this);
        viewpager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewpager);
//        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //在界面销毁的地方要解绑
        EventBus.getDefault().unregister(this);
    }
    @Subscribe
    public void getEventBus(MyProInfo info) {
        if (info != null) {
            mInfo = info;
            adapter.notifyDataSetChanged();
            if(mInfo!=null)
            {
                tv_proname.setText(mInfo.getProjectName());
                tv_manageaddr.setText(mInfo.getManagerListStr());
                tv_manageuser.setText(mInfo.getDebtorLegalRepresentative());

                String url = mInfo.getProjectLogo();

                Glide.with(this).load(url)
                        .bitmapTransform(new CropCircleTransformation(this))
                        .placeholder(R.drawable.avatar_defult)
                        .into(iv_proimg);
            }
            //这里拿到事件之后吐司一下
            //            Toast.makeText(getActivity(), "num" + num, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public Fragment getFragment(int position) {
        switch (position)
        {
            case 0:
                return ProDetailFragment.newInstance(position,mInfo);
            case 1:
                return ProZQInfoFragment.newInstance(position,mInfo.getId());
            case 2:
                return ProZQSetFragment.newInstance(position,mInfo.getId());
//                break;
            case 3:
                return ProVoteFragment.newInstance(position,mInfo.getId());

            case 4:
                return ProTalkFragment.newInstance(position,mInfo.getId());
            case 5:
                return ProTaskFragment.newInstance(position,mInfo.getId());
            case 6:
                return ProPublicFragment.newInstance(position,mInfo.getId());
                default:

                    return ProFlowFragment.newInstance(position,mInfo.getId());
//                    break;
        }

    }
}
